package com.qeado.report.web;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qeado.common.helper.AttributeHelper;
import com.qeado.report.model.ProdExport;
import com.qeado.report.service.ExportProdInfoService;

@Controller
public class ExportProdInfoControler {
	@Autowired
	private ExportProdInfoService exportProdInfoService;
	
	@RequestMapping("/prodInfoExport.ac")
	public String prodInfoExport(HttpServletRequest request){
		AttributeHelper.removeSessionAttribute(request, "T_");
		List<ProdExport> brandList = exportProdInfoService.searchBrandList();
		List<ProdExport> prodTypeList = exportProdInfoService.searchProdTypeList();
		request.getSession().setAttribute("T_brandList", brandList);
		request.getSession().setAttribute("T_prodTypeList", prodTypeList);
		return "report/prod_info_export";
	}
	
	@RequestMapping("/prodInfoExportSearch.ac")
	public String prodInfoExportSearch(HttpServletRequest request, ProdExport prodExport) {
		List<ProdExport> prodExportList = exportProdInfoService.prodInfoExportSearch(prodExport);
		request.setAttribute("prodExportList", prodExportList);
		return "report/prod_info_export";
	}
	
	@RequestMapping("/prodInfoToExcel.ac")
	public void prodInfoToExcel(HttpServletResponse response, ProdExport prodExport) {
		try {
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("content-disposition", "attachment;filename="+new String("商品信息".getBytes("GB2312"),"ISO8859-1")+".xls");
			OutputStream out = response.getOutputStream();
			WritableWorkbook book = exportProdInfoService.prodInfoToExcel(prodExport, out);
			book.write();
			book.close();
			out.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}
	}
}
